﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class PersonPlus
{
public:
	PersonPlus() {}
	PersonPlus(int a, int b) :m_A(a), m_B(b) {}

	//1.+号运算符重载，成员函数；二元运算符重载，成员函数要+1，才是二元；
	/*PersonPlus operator+(PersonPlus& p)
	{
		PersonPlus tmp;
		tmp.m_A = this->m_A + p.m_A;
		tmp.m_B = this->m_B + p.m_B;
		return tmp;
	}*/

	int m_A;
	int m_B;
};
//2.利用全局函数，进行+号运算符的重载；二元运算符重载；
PersonPlus operator+(PersonPlus& p1, PersonPlus& p2)
{
	PersonPlus tmp;
	tmp.m_A = p1.m_A + p2.m_A;
	tmp.m_B = p1.m_B + p2.m_B;
	return tmp;
}
PersonPlus operator+(PersonPlus& p1, int a)
{
	PersonPlus tmp;
	tmp.m_A = p1.m_A + a;
	tmp.m_B = p1.m_B + a;
	return tmp;
}

void testPlus()
{
	PersonPlus p1(10, 10);
	PersonPlus p2(20, 20);

	//3.p1+p2 <==> p1.operator+(p2);  或者  operator+(p1,p2); 是从这种表达式转变的；
	PersonPlus p3 = p1 + p2;
	cout << "p3的m_A：" << p3.m_A << "  m_B：" << p3.m_B << endl;//p3的m_A：30  m_B：30

	//4.运算符重载；
	PersonPlus p4 = p1 + 10;
	cout << "p4的m_A：" << p4.m_A << "  m_B：" << p4.m_B << endl;//p4的m_A：20  m_B：20

	//operator+(p1, p2);
}

//main277：加号运算符重载
int main277()
{
	testPlus();

	system("pause");
	return EXIT_SUCCESS;
}